﻿Imports iComm
Public Class InitService

    Private iDirectory As New iComm.ClsDirectory()
    Private iFS() As ClsWatcher
    Private comm As New ClsComm

    ''' <summary>
    ''' 監視対象の初期化処理
    ''' </summary>
    ''' <remarks></remarks>
    Public Function initSetting() As Boolean
        Dim initComm As New ClsComm
        Try
            Return initComm.init()

        Catch ex As Exception
            SystemError.SystemLog(ex)
            Return False
        Finally
            initComm = Nothing
        End Try
    End Function

    ''' <summary>
    ''' 監視対象の初期化処理
    ''' </summary>
    ''' <remarks></remarks>
    Public Function initRoot() As Boolean
        Dim tblRoot As New DataTable
        Dim isAuto As Boolean = False
        Dim cnt As Integer = -1
        Try

            If iFS IsNot Nothing Then
                For i = 0 To iFS.Count - 1
                    iFS(i).watcher.EnableRaisingEvents = False
                    iFS(i) = Nothing
                Next
            End If
            iFS = Nothing

            iDirectory.selectRootFromDB(tblRoot)
            If tblRoot Is Nothing OrElse tblRoot.Rows.Count = 0 Then
                Comm.WriteToEventLog("監視対象がない。", ClsStatic.EventID.Warning)
                Return True
            End If

            For i = 0 To tblRoot.Rows.Count - 1
                '自動監視対象か
                isAuto = tblRoot.Rows(i)(2)
                If isAuto = False Then Continue For

                cnt += 1
                ReDim Preserve iFS(cnt)
                'pathを取得
                iFS(i) = New ClsWatcher(tblRoot.Rows(i)(1))

                '監視を開始する
                iFS(i).watcher.EnableRaisingEvents = True
            Next

            Return True
        Catch ex As Exception
            SystemError.SystemLog(ex)
            Return False
        Finally
            tblRoot = Nothing
        End Try
    End Function
End Class
